package com.libbase.manager;

import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.LiveDataReactiveStreams;

import com.chan.net.DefaultLiveDataObserver;
import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.libbase.bean.NyqfBean;
import com.libbase.http.Http;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okio.ByteString;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class NyqfManager {

    public static String getnyqfTitles(LifecycleOwner lifecycleOwner, HttpCallback httpCallback) {
        String request_tag = "getnyqfTitles" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getNyqfRetrofit(NyqfApi.class);
        Flowable<Result<NyqfBean>> observable = api.getnyqfTitles().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        LiveData mLiveData = LiveDataReactiveStreams.fromPublisher(observable);
        mLiveData.observe(lifecycleOwner, DefaultLiveDataObserver.obtain(request_tag, httpCallback));
        return request_tag;
    }

    /**
     * 南粤清风News信息
     *
     * @param nav
     * @param lifecycleOwner
     * @param httpCallback
     * @return
     */
    public static String getnyqfNews(String nav, LifecycleOwner lifecycleOwner, HttpCallback httpCallback) {
        String request_tag = "getnyqfNews" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        NyqfApi api = Http.getNyqfRetrofit(NyqfApi.class);
        Flowable<Result<NyqfBean>> observable = api.getnyqfNews(nav).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        LiveData mLiveData = LiveDataReactiveStreams.fromPublisher(observable);
        mLiveData.observe(lifecycleOwner,  DefaultLiveDataObserver.obtain(request_tag, httpCallback));
        return request_tag;
    }


    private interface NyqfApi {
        @GET("raw/master/nyqf_data.json")
        Flowable<Result<NyqfBean>> getnyqfNews(@Query("nav") String nav);

        @GET("raw/master/nyqf_titles.json")
        Flowable<Result<NyqfBean>> getnyqfTitles();
    }
}
